package com.micangcaihang.micang.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.tools.ScreenUtils;
import com.micangcaihang.common.base.popup.BasePopupWindow;
import com.micangcaihang.common.utils.ConvertUtils;
import com.micangcaihang.common.utils.TimeUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.entity.ReBackEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/8/14
 *     update : 2017/8/14
 *     desc   : MiCang
 *     version: 1.0
 * </pre>
 */
public class ReBackPopup extends BasePopupWindow {

    private RecyclerView mRecyclerView;
    private ReBackAdapter mAdapter;
    private int popHeight;
    private Context mContext;

    public ReBackAdapter getAdapter() {
        return mAdapter;
    }

    public ReBackPopup(Activity context) {
        super(context);
        mContext = context;
        setBackPressEnable(false);
        setDismissWhenTouchOuside(true);
        popHeight = ScreenUtils.getScreenHeight(context) / 2;
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        mAdapter = new ReBackAdapter(R.layout.item_pop_back_reason, new ArrayList<>());
        mRecyclerView.setAdapter(mAdapter);
    }

    public void notifyAdapter(List<ReBackEntity> list) {
        mRecyclerView.measure(0,0);
        int rvHeight = list.size() * ConvertUtils.dp2px(120);
        if (rvHeight > popHeight) {
            ViewGroup.LayoutParams layoutParams = mRecyclerView.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = popHeight;
            mRecyclerView.setLayoutParams(layoutParams);
        }
        mAdapter.setNewData(list);
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.pop_back_reason);
    }

    @Override
    public void showPopupWindow(View v) {
        super.showPopupWindow(v);
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }

    @Override
    protected Animation initShowAnimation() {
        TranslateAnimation translateAnimation = new TranslateAnimation(0f, 0f, -ConvertUtils.dp2px(350f), 0);
        translateAnimation.setDuration(300);
        translateAnimation.setInterpolator(new OvershootInterpolator(1));
        return translateAnimation;
    }

    @Override
    protected Animation initExitAnimation() {
        TranslateAnimation translateAnimation = new TranslateAnimation(0f, 0f, 0, -ConvertUtils.dp2px(350f));
        translateAnimation.setDuration(300);
        translateAnimation.setInterpolator(new OvershootInterpolator(-4));
        return translateAnimation;
    }

    /**
     * 设置一个点击后触发dismiss PopupWindow的View，一般为蒙层
     */
    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    public class ReBackAdapter extends BaseQuickAdapter<ReBackEntity, BaseViewHolder> {

        public ReBackAdapter(@LayoutRes int layoutResId, @Nullable List<ReBackEntity> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ReBackEntity item) {
            helper.setText(R.id.tv_back_department, String.format(mContext.getString(R.string.format_reback_department), item.getDepartment_name()));
            helper.setText(R.id.tv_bacn_reson, item.getReback_reason());
            try{
                String s = TimeUtils.millis2StringMinute(Long.parseLong(item.getReback_time()) * 1000);
                helper.setText(R.id.tv_back_time, s);
            }catch (Exception e){
                helper.setText(R.id.tv_back_time, "");
            }
        }
    }
}
